home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SortItem.java < prev    next >
Text File  |  1998-09-15  |  7KB  |  276 lines

  1. /*
  2.  * @(#)SortItem.java    1.5 97/07/30
  3.  *
  4.  * Copyright (c) 2070, 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.io.InputStream;
  34. import java.util.Hashtable;
  35. import java.net.*;
  36.  
  37. /**
  38.  * A simple applet class to demonstrate a sort algorithm.
  39.  * You can specify a sorting algorithm using the "alg"
  40.  * attribyte. When you click on the applet, a thread is
  41.  * forked which animates the sorting algorithm.
  42.  *
  43.  * @author James Gosling
  44.  * @version     1.17f, 10 Apr 1995
  45.  */
  46. public class SortItem 
  47.     extends java.applet.Applet 
  48.     implements Runnable, MouseListener {
  49.     /**
  50.      * The thread that is sorting (or null).
  51.      */
  52.     private Thread kicker;
  53.  
  54.     /**
  55.      * The array that is being sorted.
  56.      */
  57.     int arr[];
  58.  
  59.     /**
  60.      * The high water mark.
  61.      */
  62.     int h1 = -1;
  63.  
  64.     /**
  65.      * The low water mark.
  66.      */
  67.     int h2 = -1;
  68.  
  69.     /**
  70.      * The name of the algorithm.
  71.      */
  72.     String algName;
  73.  
  74.     /**
  75.      * The sorting algorithm (or null).
  76.      */
  77.     SortAlgorithm algorithm;
  78.  
  79.     /**
  80.      * Fill the array with random numbers from 0..n-1.
  81.      */
  82.     void scramble() {
  83.     int a[] = new int[getSize().height / 2];
  84.     double f = getSize().width / (double) a.length;
  85.     for (int i = a.length; --i >= 0;) {
  86.         a[i] = (int)(i * f);
  87.     }
  88.     for (int i = a.length; --i >= 0;) {
  89.         int j = (int)(i * Math.random());
  90.         int t = a[i];
  91.         a[i] = a[j];
  92.         a[j] = t;
  93.     }
  94.     arr = a;
  95.     }
  96.  
  97.     /**
  98.      * Pause a while.
  99.      * @see SortAlgorithm
  100.      */
  101.     void pause() {
  102.     pause(-1, -1);
  103.     }
  104.  
  105.     /**
  106.      * Pause a while, and draw the high water mark.
  107.      * @see SortAlgorithm
  108.      */
  109.     void pause(int H1) {
  110.     pause(H1, -1);
  111.     }
  112.  
  113.     /**
  114.      * Pause a while, and draw the low&high water marks.
  115.      * @see SortAlgorithm
  116.      */
  117.     void pause(int H1, int H2) {
  118.     h1 = H1;
  119.     h2 = H2;
  120.     if (kicker != null) {
  121.         repaint();
  122.     }
  123.     try {Thread.sleep(20);} catch (InterruptedException e){}
  124.     }
  125.  
  126.     /**
  127.      * Initialize the applet.
  128.      */
  129.     public void init() {
  130.     String at = getParameter("alg");
  131.     if (at == null) {
  132.         at = "BubbleSort";
  133.     }
  134.  
  135.     algName = at + "Algorithm";
  136.     scramble();
  137.  
  138.     resize(100, 100);
  139.     addMouseListener(this);
  140.     }
  141.  
  142.     public void start() {
  143.         scramble();
  144.         repaint();
  145.     }
  146.  
  147.     /**
  148.      * Deallocate resources of applet.
  149.      */
  150.     public void destroy() {
  151.         removeMouseListener(this);
  152.     }
  153.  
  154.     /**
  155.      * Paint the array of numbers as a list
  156.      * of horizontal lines of varying lengths.
  157.      */
  158.     public void paint(Graphics g) {
  159.     int a[] = arr;
  160.     int y = getSize().height - 1;
  161.  
  162.     // Erase old lines
  163.     g.setColor(getBackground());
  164.     for (int i = a.length; --i >= 0; y -= 2) {
  165.         g.drawLine(arr[i], y, getSize().width, y);
  166.     }
  167.  
  168.     // Draw new lines
  169.     g.setColor(Color.black);
  170.     y = getSize().height - 1;
  171.     for (int i = a.length; --i >= 0; y -= 2) {
  172.         g.drawLine(0, y, arr[i], y);
  173.     }
  174.  
  175.     if (h1 >= 0) {
  176.         g.setColor(Color.red);
  177.         y = h1 * 2 + 1;
  178.         g.drawLine(0, y, getSize().width, y);
  179.     }
  180.     if (h2 >= 0) {
  181.         g.setColor(Color.blue);
  182.         y = h2 * 2 + 1;
  183.         g.drawLine(0, y, getSize().width, y);
  184.     }
  185.     }
  186.  
  187.     /**
  188.      * Update without erasing the background.
  189.      */
  190.     public void update(Graphics g) {
  191.     paint(g);
  192.     }
  193.  
  194.     /**
  195.      * Run the sorting algorithm. This method is
  196.      * called by class Thread once the sorting algorithm
  197.      * is started.
  198.      * @see java.lang.Thread#run
  199.      * @see SortItem#mouseUp
  200.      */
  201.     public void run() {
  202.     try {
  203.         if (algorithm == null) {
  204.         algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
  205.         algorithm.setParent(this);
  206.         }
  207.         algorithm.init();
  208.         algorithm.sort(arr);
  209.     } catch(Exception e) {
  210.     }
  211.     }
  212.  
  213.     /**
  214.      * Stop the applet. Kill any sorting algorithm that
  215.      * is still sorting.
  216.      */
  217.     public synchronized void stop() {
  218.     if (algorithm != null){
  219.             try {
  220.         algorithm.stop();
  221.             } catch (IllegalThreadStateException e) {
  222.                 // ignore this exception
  223.             }
  224.             kicker = null;
  225.     }
  226.     }
  227.  
  228.     /**
  229.      * For a Thread to actually do the sorting. This routine makes
  230.      * sure we do not simultaneously start several sorts if the user
  231.      * repeatedly clicks on the sort item.  It needs to be
  232.      * synchronoized with the stop() method because they both
  233.      * manipulate the common kicker variable.
  234.      */
  235.     private synchronized void startSort() {
  236.     if (kicker == null || !kicker.isAlive()) {
  237.         kicker = new Thread(this);
  238.         kicker.start();
  239.     }
  240.     }
  241.  
  242.   //1.1 event handling
  243.   
  244.   public void mouseClicked(MouseEvent e)
  245.   {}
  246.      
  247.   public void mousePressed(MouseEvent e)
  248.   {}
  249.  
  250.   /**
  251.    * The user clicked in the applet. Start the clock!
  252.    */
  253.   
  254.   public void mouseReleased(MouseEvent e) {
  255.     startSort();
  256.     e.consume();
  257.   }
  258.       
  259.   public void mouseEntered(MouseEvent e)
  260.   {}
  261.       
  262.   public void mouseExited(MouseEvent e)     
  263.   {}
  264.  
  265.   public String getAppletInfo() {
  266.     return "Title: ImageMap \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm.  \nYou can specify a sorting algorithm using the 'alg' attribyte.  \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
  267.   }
  268.   
  269.   public String[][] getParameterInfo() {
  270.     String[][] info = {
  271.       {"als", "string", "The name of the algorithm to run.  You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.'  BubbleSort is the default.  Example:  Use 'QSort' to run the QSortAlgorithm class."}
  272.     };
  273.     return info;
  274.   }
  275. }
  276.